<template>
  <el-form label-width="100px">
    <el-form-item label="标题">
      <el-input
          v-model="rowData.title"
          placeholder="请输入标题"
          type="textarea"
      >
      </el-input>
    </el-form-item>
    <el-form-item label="内容">
      <el-input
          v-model="rowData.content"
          :autosize="{ minRows: 5}"
          placeholder="请输入内容"
          type="textarea">
      </el-input>
    </el-form-item>
    <el-form-item style="text-align: right">
      <el-button plain type="danger">取消</el-button>
      <el-button plain type="primary" @click="submit()">{{ mode }}</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {copyObj} from "../../common/utils";
import {AddAnnouncement, UpdateAnnouncement} from "../../network/announcementApi";

export default {
  name: "dialogAnnouncement",
  data() {
    return {
      rowData: {}
    }
  },
  methods: {
    updateAnnouncement() {
      let d = this.rowData
      console.log(d)
      UpdateAnnouncement(d).then(res => {
        console.log(res)
        if (res.code === 2000) {
          this.$emit("success")
        }
      })
    },
    submit() {
      let c = this.mode;
      if (c != "添加") {
        this.updateAnnouncement();
      } else {
        let d = {};
        d.title = this.rowData.title;
        d.content = this.rowData.content;
        AddAnnouncement(d).then(res => {
          console.log(res)
          if (res.code === 2000) {
            this.$emit("success")
          }
        })
      }
    },
  },
  mounted() {
    this.rowData = copyObj(this.row)

  },
  watch: {
    "row": {
      handler: function (d) {
        this.rowData = copyObj(d)
      }
    }
  },

  props: ["row", "mode"],
}
</script>

<style scoped>

</style>